package com.example.compose.utils

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.StrictMode
import java.io.File


object ShareManager {

    init {
        val builder = StrictMode.VmPolicy.Builder()
        StrictMode.setVmPolicy(builder.build())
        builder.detectFileUriExposure()
    }

    //分享文件
    fun shareFiles(context: Context?, fileList: List<File?>?) {
        if (context == null || fileList == null || fileList.isEmpty()) {
            return
        }
        val uriList: MutableList<Uri?> = ArrayList()
        for (file in fileList) {
            val uri = Uri.fromFile(file)
            uriList.add(uri)
        }
        var intent: Intent? = null
        val isMultiple = uriList.size > 1
        //是否批量分享
        if (isMultiple) {
            intent = Intent(Intent.ACTION_SEND_MULTIPLE)
            intent.type = "**"
            intent.putExtra(Intent.EXTRA_STREAM, uriList[0])
        }
        context.startActivity(
            Intent.createChooser(
                intent,
                "Choose a channel to share your files..."
            )
        )
    }

    //分享图片
    fun shareImage(context: Context?, imageFile: File?) {
        if (context == null || imageFile == null) {
            return
        }

        val intent = Intent(Intent.ACTION_SEND)
        intent.type = "image/*"
        val uri = Uri.fromFile(imageFile)
        intent.putExtra(Intent.EXTRA_STREAM, uri)
        context.startActivity(
            Intent.createChooser(
                intent,
                "Choose a channel to share your image..."
            )
        )
    }

    //分享文字
    fun shareText(context: Context?, text: String?) {
        if (context == null || text == null) {
            return
        }
        val intent = Intent(Intent.ACTION_SEND)
        intent.type = "text/plain"
        intent.putExtra(Intent.EXTRA_TEXT, text)
        context.startActivity(
            Intent.createChooser(
                intent,
                "Choose a channel to share your text..."
            )
        )
    }
}